<template>
<div class="product-list">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-tabs v-model="activeName" @tab-change="handleClick">
        <el-tab-pane :label="$t('product.on_sale')" name="sell">
          <template #label>
          <span >{{$t('product.on_sale')}} <el-tag size="mini">{{productCount.sell}}</el-tag></span>
          </template>
        </el-tab-pane>
        <el-tab-pane :label="$t('product.in_warehouse')" name="lower">
          <template #label>
          <span >{{$t('product.in_warehouse')}} <el-tag size="mini">{{productCount.lower}}</el-tag></span>
          </template>
        </el-tab-pane>
        <el-tab-pane :label="$t('product.in_recycle_bin')" name="recovery">
            <template #label>
          <span >{{$t('product.in_recycle_bin')}} <el-tag size="mini">{{productCount.recovery}}</el-tag></span>
          </template>
        </el-tab-pane>
        <el-tab-pane :label="$t('product.pending_review')" name="audit">
          <template #label>
          <span >{{$t('product.pending_review')}} <el-tag size="mini">{{productCount.audit}}</el-tag></span>
          </template>
        </el-tab-pane>
        <el-tab-pane :label="$t('product.review_rejected_alt')" name="noAudit">
           <template #label>
          <span >{{$t('product.review_rejected_alt')}} <el-tag size="mini">{{productCount.noAudit}}</el-tag></span>
          </template>
        </el-tab-pane>
      </el-tabs>
      <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('product.product_category')">
          <el-select size="small" v-model="searchForm.categoryId" :placeholder="$t('product.all_categories')">
            <el-option :label="$t('product.all')" value="0"></el-option>
            <el-option v-for="(item, index) in categoryList" :key="index" :label="item.name" :value="item.categoryId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('product.product_name')"><el-input size="small" v-model="searchForm.productName" :placeholder="$t('product.input_product_name')"></el-input></el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="Search" @click="onSubmit">{{$t('product.search')}}</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--添加产品--> 
    <!-- <div class="common-level-rail"><el-button size="small" type="primary" icon="el-icon-plus" @click="addClick" v-auth="'/product/product/add'">添加产品</el-button></div> -->
    <div class="common-level-rail" v-if="activeName == 'recovery'">
      <el-button size="small" type="primary" v-auth="'/product/product/add'" @click="allDelClick()">{{$t('product.one_click_delete')}}</el-button>
    </div>

    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading"  @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="productName" :label="$t('product.product3')" width="400px">
            <template  #default="scope">
              <div class="product-info">
                <div class="pic"><img v-img-url="scope.row.imagePath" alt="" /></div>
                <div class="info">
                  <div class="name">{{scope.row.productName}}</div>
                  <div class="price">￥{{scope.row.productPrice}}</div>
                  <div class="price" v-if="activeName == 'noAudit'">{{$t('product.rejected_reason',{place1:scope.row.auditRemark})}}</div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="categoryName" :label="$t('product.category')"></el-table-column>
          <el-table-column prop="supplierName" :label="$t('product.store_owner')"></el-table-column>
          <el-table-column prop="salesActual" :label="$t('product.actual_sales')"></el-table-column>
          <el-table-column prop="productStock" :label="$t('product.stock')"></el-table-column>
          <el-table-column prop="viewTimes" :label="$t('product.click_count')"></el-table-column>
          <el-table-column :label="$t('product.product_attributes')">
            <template  #default="scope">
              <span v-if="scope.row.isVirtual == 0">{{$t('product.physical_product')}}</span>
              <span v-if="scope.row.isVirtual == 1" class="green">{{$t('product.virtual_product')}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" :label="$t('product.publish_time')" width="130"></el-table-column>
          <el-table-column prop="productSort" :label="$t('product.sort')"></el-table-column>
          <el-table-column fixed="right" :label="$t('product.action')" width="80">
            <template  #default="scope">
              <div class="table-btn-column">
                <el-button v-if="scope.row.auditStatus == 10" @click="editClick(scope.row)" type="text" size="small" v-auth="'/product/product/edit'">{{$t('product.edit_product')}}</el-button>
                <el-button v-if="scope.row.auditStatus == 0" @click="editClick(scope.row)" type="text" size="small" v-auth="'/product/product/edit'">{{$t('product.review_product')}}</el-button>
                <div>
                  <el-button @click="delClick(scope.row)" type="text" size="small" v-auth="'/product/product/delete'">{{$t('product.delete_product')}}</el-button>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--分页-->
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="curPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import PorductApi from '@/api/product.js';
export default {
  components: {},
  data() {
    return {
      /*切换菜单*/
      activeName: 'sell',
      /*切换选中值*/
      activeIndex: '0',
      /*是否正在加载*/
      loading: true,
      /*一页多少条*/
      pageSize: 15,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*搜索参数*/
      searchForm: {
        productName: '',
        categoryId: ''
      },
      /*列表数据*/
      tableData: [],
      /*全部分类*/
      categoryList: [],
      productCount: {},
      multipleSelection: []
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    handleSelectionChange(e) {
      this.multipleSelection = e;
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.loading = true;
      self.curPage = val;
      self.getData();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    /*切换菜单*/
    handleClick(tab, event) {
      let self = this;
      self.curPage = 1;
      self.multipleSelection = [];
      self.getData();
    },
    /*获取列表*/
    getData() {
      let self = this;
      let Params = self.searchForm;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      Params.type = self.activeName;
      self.loading = true;
      PorductApi.productList(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.productList.records;
        self.categoryList = res.data.categoryList;
        self.totalDataNumber = res.data.productList.total;
        self.productCount = res.data.productCount;
      }).catch(error => {
        self.loading = false;
      });
    },
    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.getData();
    },
    /*打开添加*/
    addClick() {
      this.$router.push('/product/product/add');
    },
    /*打开编辑*/
    editClick(row) {
      console.log(row);
      this.$router.push({
        path: '/product/product/edit',
        query: {
          productId: row.productId,
          scene: 'edit'
        }
      });
    },
    linkClick: function (val) {
      let self = this;
      let copyText = 'pages/product/detail/detail?productId=' + val;
      self.$copyText(copyText).then(function (e) {
        ElMessage({
          showClose: true,
          message: $t("product.copy_success"),
          type: 'success'
        });
      }, function (e) {
        ElMessage({
          showClose: true,
          message: $t("product.copy_fail"),
          type: 'error'
        });
      });
    },
    /*打开复制*/
    copyClick(row) {
      this.$router.push({
        path: '/product/product/edit',
        query: {
          productId: row.productId,
          scene: 'copy'
        }
      });
    },
    /*删除*/
    delClick: function (row) {
      let self = this;
      let msg = '';
      if (row.productStatus == 30) {
        msg = $t("product.delete_confirm");
      } else {
        msg = $t("product.confirm_delete_to_recycle");
      }
      ElMessageBox.confirm(msg, $t("product.tip"), {
        type: 'warning'
      }).then(() => {
        PorductApi.delProduct({
          productId: row.productId
        }).then(data => {
          ElMessage({
            message: $t("product.delete_success"),
            type: 'success'
          });
          self.getData();
        });
      });
    },
    /*多选删除*/
    allDelClick() {
      let ids = [];
      let self = this;
      self.multipleSelection.forEach(v => {
        ids.push(v.productId);
      });
      ids = ids.join(",");
      ElMessageBox.confirm($t("product.delete_confirm"), $t("product.tip"), {
        type: 'warning'
      }).then(() => {
        PorductApi.allDelProduct({
          productIds: ids
        }).then(data => {
          ElMessage({
            message: $t("product.delete_success"),
            type: 'success'
          });
          self.getData();
        });
      });
    },
    /*修改状态*/
    changeStatus(item, value) {
      let self = this;
      let msg = '';
      if (value == 10) {
        msg = $t("product.confirm_publish_product");
      } else {
        msg = $t("product.confirm_unpublish_product");
        if (self.activeName == 'recovery') {
          msg = $t("product.confirm_move_to_warehouse");
        }
      }
      ElMessageBox.confirm(msg, $t("product.tip"), {
        type: 'warning'
      }).then(() => {
        PorductApi.changeStatus({
          productId: item.productId,
          state: value
        }).then(data => {
          ElMessage({
            message: $t("product.action_success"),
            type: 'success'
          });
          self.getData();
        });
      });
    }
  }
};
</script>

